﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib.CodeParser;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    public class TestCodeParser
    {
        //[Test]
        public void TestSplitSingleLine()
        {
            string strLine = @"( NAME = N'AdventureWorks_1.1_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_1.1_Data.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 16384KB )";

            DBSourceToolsLib.CodeParser.CodeParser oParser = new DBSourceToolsLib.CodeParser.CodeParser();
            oParser.BuildStack(strLine, 0);

            //for (int i = 0; i < oParser.LstParseObject.Count; i++)
            //{
            //    Console.WriteLine(i.ToString() + " " + oParser.LstParseObject[i].DumpToString());
            //}

        }

        [Test]
        public void TestSplitSQLLine()
        {
            string strLine = @"select * from TestData;";

            DBSourceToolsLib.CodeParser.CodeParser oParser = new CodeParser();
            oParser.BuildStack(strLine, 0);

            string strResults = oParser.BuildFullString();

            Console.WriteLine(strResults);

            Assert.AreEqual(
@"0 VERB |select| 1 [0,0]
1 SPACE | | 1 [0,1]
2 TOKEN |*| 1 [0,2]
3 SPACE | | 1 [0,3]
4 VERB |from| 1 [0,4]
5 SPACE | | 1 [0,5]
6 UNKNOWN |TestData| 0 [0,6]
7 TOKEN |;| 1 [0,7]
", strResults);
        }

        [Test]
        public void TestSplitSQLLine2()
        {
            string strLine = @"select TestData from TestData;";

            DBSourceToolsLib.CodeParser.CodeParser oParser = new CodeParser();
            oParser.BuildStack(strLine, 0);

            string strResults = oParser.BuildFullString();

            Console.WriteLine(strResults);

            Assert.AreEqual(
@"0 VERB |select| 1 [0,0]
1 SPACE | | 1 [0,1]
2 UNKNOWN |TestData| 0 [0,2]
3 SPACE | | 1 [0,3]
4 VERB |from| 1 [0,4]
5 SPACE | | 1 [0,5]
6 UNKNOWN |TestData| 0 [0,6]
7 TOKEN |;| 1 [0,7]
", strResults);

        }


        [Test]
        public void TestSplitSQLLine2_TwoLines()
        {
            string strLine = @"select TestData
from TestData;";

            DBSourceToolsLib.CodeParser.CodeParser oParser = new CodeParser();
            oParser.BuildStack(strLine,0);
            string strResults = oParser.BuildFullString();

            Console.WriteLine(strResults);

            Assert.AreEqual(
@"0 VERB |select| 1 [0,0]
1 SPACE | | 1 [0,1]
2 UNKNOWN |TestData
from| 0 [0,2]
3 SPACE | | 1 [0,3]
4 UNKNOWN |TestData| 0 [0,4]
5 TOKEN |;| 1 [0,5]
", strResults);

        }


        [Test]
        public void TestSplitSQLLineBraces()
        {
            string strLine = @"select (TestData, TestObject, 'Test') from TestData;";

            DBSourceToolsLib.CodeParser.CodeParser oParser = new CodeParser();
            oParser.BuildStack(strLine, 0);
            string strResults = oParser.BuildFullString();

            Console.WriteLine(strResults);

            Assert.AreEqual(
@"0 VERB |select| 1 [0,0]
1 SPACE | | 1 [0,1]
2 BRACE |(| 0 [0,2] end
3 UNKNOWN |TestData| 0 [0,3]
4 TOKEN |,| 1 [0,4]
5 SPACE | | 1 [0,5]
6 UNKNOWN |TestObject| 0 [0,6]
7 TOKEN |,| 1 [0,7]
8 SPACE | | 1 [0,8]
9 BRACE |'| 0 [0,9] end
10 UNKNOWN |Test| 0 [0,10]
11 BRACE |'| 0 [0,11] end
12 BRACE |)| 0 [0,12] end
13 SPACE | | 1 [0,13]
14 VERB |from| 1 [0,14]
15 SPACE | | 1 [0,15]
16 UNKNOWN |TestData| 0 [0,16]
17 TOKEN |;| 1 [0,17]
", strResults);
        }

        [Test]
        public void TestSourceFile()
        {
            SourceFile oSourceFile = new SourceFile();
//            oSourceFile.setText(@"select ('literal1', 'literal2', TestData) from TestData;");
//            Console.WriteLine(oSourceFile.DumpToString());
            oSourceFile.setText(@"select (TestData,

'testLiteral')

from TestData;");

            string strExpected = @"Line
VERB |select| 1 [0,0]
SPACE | | 1 [0,1]
BRACE |(| 1 [0,2]-[2,3] beg
UNKNOWN |TestData| 0 [0,3]
TOKEN |,| 1 [0,4]
NEWLINE crlf |
| 1 [0,5]

Line
NEWLINE crlf |
| 1 [1,0]

Line
BRACE |'| 1 [2,0]-[2,2] beg
UNKNOWN |testLiteral| 0 [2,1]
BRACE |'| 1 [2,2]-[2,0] end
BRACE |)| 1 [2,3]-[0,2] end
NEWLINE crlf |
| 1 [2,4]

Line
NEWLINE crlf |
| 1 [3,0]

Line
VERB |from| 1 [4,0]
SPACE | | 1 [4,1]
UNKNOWN |TestData| 0 [4,2]
TOKEN |;| 1 [4,3]

";
            Console.WriteLine("=========== before ");
            Console.WriteLine(oSourceFile.DumpToString());

            CodeParser oCodeParser = new CodeParser();
            oCodeParser.ResolveBraces(ref oSourceFile);
            Console.WriteLine("=========== after ");
            string strResults = oSourceFile.DumpToString();
            Console.WriteLine(strResults);

            Assert.AreEqual(strExpected, strResults); 

        }

        [Test]
        public void TestUnmatchedQuote()
        {
            SourceFile oSourceFile = new SourceFile();
            oSourceFile.setText(@"select TestData from 'TestData;");

            string strExpected = @"Line
VERB |select| 1 [0,0]
SPACE | | 1 [0,1]
UNKNOWN |TestData| 0 [0,2]
SPACE | | 1 [0,3]
VERB |from| 1 [0,4]
SPACE | | 1 [0,5]
BRACE |'| 1 [0,6] beg
UNKNOWN |TestData| 0 [0,7]
TOKEN |;| 1 [0,8]

";

            CodeParser oCodeParser = new CodeParser();
            oCodeParser.ResolveBraces(ref oSourceFile);
            Console.WriteLine("=========== after ");
            string strResults = oSourceFile.DumpToString();
            Console.WriteLine(strResults);

            Assert.AreEqual(strExpected, strResults);

            string strErrs = oSourceFile.GetErrorString();
            Assert.AreEqual(@"Unmatched Quote : Line 0
", strErrs);

        }

        [Test]
        public void TestWithTables()
        {
            SourceFile oSourceFile = new SourceFile();
            ParseTable.AddTable("TestData");
            oSourceFile.setText(@"TestData;");

            string strExpected = @"Line
TABLE |TestData| 1 [0,0]
TOKEN |;| 1 [0,1]

";

            CodeParser oCodeParser = new CodeParser();
            
            oCodeParser.ResolveBraces(ref oSourceFile);
            Console.WriteLine("=========== after ");
            string strResults = oSourceFile.DumpToString();
            Console.WriteLine(strResults);

            Assert.AreEqual(strExpected, strResults);

        }

        [Test]
        public void TestWithTables2()
        {
            SourceFile oSourceFile = new SourceFile();
            ParseTable.AddTable("TestData");
            oSourceFile.setText(@"select * from
 TestData");

            string strExpected = @"Line
VERB |select| 1 [0,0]
SPACE | | 1 [0,1]
TOKEN |*| 1 [0,2]
SPACE | | 1 [0,3]
VERB |from| 1 [0,4]
NEWLINE crlf |
| 1 [0,5]

Line
SPACE | | 1 [1,0]
TABLE |TestData| 1 [1,1]

";

            CodeParser oCodeParser = new CodeParser();

            oCodeParser.ResolveBraces(ref oSourceFile);
            Console.WriteLine("=========== after ");
            string strResults = oSourceFile.DumpToString();
            Console.WriteLine(strResults);

            Assert.AreEqual(strExpected, strResults);

        }

    }
}
